using System.Collections;
using I2.Loc;
using UnityEngine;
using UnityEngine.SceneManagement;

public class MapEntranceWindow : MonoBehaviour
{
	private string desireMap = string.Empty;

	public UILabel title;

	public GameObject lockGO;

	public UITexture mapTexture;

	public GameObject btnPVP;

	public GameObject btnSendbox;

	public GameObject btnAdvanture;

	public GameObject btnPVPElite;

	public GameObject btnSendboxElite;

	public GameObject btnAdvantureElite;

	public GameObject btnPVPOff;

	public GameObject btnSendboxOff;

	public GameObject btnAdvantureOff;

	private ReputationMapInfo districtInfo;

	[SerializeField]
	private MessageBox box;

	private bool mapAvailable = true;

	public void SetInfo(string nameScene)
	{
		desireMap = nameScene;
		string text = SceneManager.GetActiveScene().name;
		mapTexture.gameObject.SetActive(true);
		title.text = LocalizationManager.GetTermTranslation("PVP_map_info/lb_text_LeaveBattle");
		mapTexture.mainTexture = Resources.Load<Texture>("ReputationMap/Icons/" + nameScene);
	}

	private void SetPVPButton(bool active)
	{
		SetButtom(btnPVP, btnPVPElite, btnPVPOff, active);
	}

	private void SetSendboxButton(bool active)
	{
		SetButtom(btnSendbox, btnSendboxElite, btnSendboxOff, active);
	}

	private void SetAdvantureButton(bool active)
	{
		SetButtom(btnAdvanture, btnAdvantureElite, btnAdvantureOff, active);
	}

	private void SetButtom(GameObject normalBtn, GameObject eliteBtn, GameObject offBtn, bool active)
	{
		offBtn.SetActive(!active);
		normalBtn.SetActive(active);
		eliteBtn.SetActive(false);
	}

	private void Awake()
	{
		lockGO.SetActive(false);
		btnPVP.SetActive(false);
		btnPVPElite.SetActive(false);
		btnPVPOff.SetActive(false);
	}

	private void OnEnable()
	{
		if (settings.offlineMode)
		{
			SetSendboxButton(true);
			SetAdvantureButton(false);
		}
		else
		{
			SetSendboxButton(false);
			SetAdvantureButton(true);
		}
	}

	public void GoToPVP()
	{
		settings.SaveCathedExeptionsToStorager();
		if (ManagerMissions.currMission != null)
		{
			box.Show("Game_Pause/lb_mission_prigress_lost", GoToPVPMap);
		}
		else
		{
			GoToPVPMap();
		}
	}

	private void GoToPVPMap()
	{
		if (desireMap.Equals("Level1"))
		{
			SceneLoadBar.showHalfBar = false;
			StartCoroutine(GoToMap());
		}
		else if (districtInfo.state != DistrictState.Locked)
		{
			SceneLoadBar.showHalfBar = false;
			StartCoroutine(GoToMap());
		}
	}

	public void GoToSandbox()
	{
		settings.SaveCathedExeptionsToStorager();
		if (ManagerMissions.currMission != null)
		{
			box.Show("Game_Pause/lb_mission_prigress_lost", GoToSandboxMap);
		}
		else
		{
			GoToSandboxMap();
		}
	}

	private void GoToSandboxMap()
	{
		if (SceneManager.GetActiveScene().name.Equals("LoadingGame"))
		{
			return;
		}
		if (SceneManager.GetActiveScene().name.Equals("Level1"))
		{
			MessageWindow.instance.ToggleBlockPanel(true);
			if (PrivateCarManager.instance != null)
			{
				PrivateCarManager.instance.UnEquipCar();
			}
			if (GameController.instance != null)
			{
				GameController.instance.playerScript.ExplosionAllMine();
				GameController.instance.playerScript.ExplosionAllGrenade();
			}
			if (FyberWrapper.instance != null)
			{
				FyberWrapper.instance.ShowAds(ADSLocation.TransilitionToThePost);
			}
			controllerConnectPhoton.thisScript.chooseNameLevel = SceneManager.GetActiveScene().name;
			controllerConnectPhoton.thisScript.changeMap = true;
			controllerConnectGame.thisScript.SetOnline(true);
		}
		else
		{
			SceneLoadBar.showHalfBar = false;
			StartCoroutine(GoToMap());
		}
		base.gameObject.SetActive(false);
	}

	public void GoToAdvanture()
	{
		settings.SaveCathedExeptionsToStorager();
		if (ManagerMissions.currMission != null)
		{
			box.Show("Game_Pause/lb_mission_prigress_lost", GoToAdvantureMap);
		}
		else
		{
			GoToAdvantureMap();
		}
	}

	private void GoToAdvantureMap()
	{
		if (SceneManager.GetActiveScene().name.Equals("Level1"))
		{
			if (PrivateCarManager.instance != null)
			{
				PrivateCarManager.instance.UnEquipCar();
			}
			if (GameController.instance != null)
			{
				GameController.instance.playerScript.ExplosionAllMine();
				GameController.instance.playerScript.ExplosionAllGrenade();
			}
			if (FyberWrapper.instance != null)
			{
				FyberWrapper.instance.ShowAds(ADSLocation.TransilitionToThePost);
			}
			controllerConnectPhoton.thisScript.chooseNameLevel = SceneManager.GetActiveScene().name;
			controllerConnectPhoton.thisScript.changeMap = true;
			controllerConnectGame.thisScript.SetOnline(false);
			Invoke("WaitForRespBonuse", 1f);
		}
		else
		{
			settings.SetAreaNameForRespawn(SceneManager.GetActiveScene().name);
			settings.updateOfflineMode(true);
			if (FyberWrapper.instance != null)
			{
				FyberWrapper.instance.ShowAds(ADSLocation.TransilitionToThePost);
			}
			PhotonNetwork.Disconnect();
			MessageWindow.instance.SetVisiblePanelLoading(true, string.Empty);
			settings.keySelectScene = "Level1";
			SceneManager.LoadSceneAsync("LoadingGame");
		}
		base.gameObject.SetActive(false);
	}

	private void WaitForRespBonuse()
	{
		if (bonuseManager.thisScript.listAllBonuse.Count == 0)
		{
			bonuseManager.thisScript.SpawnBonuses();
		}
	}

	private IEnumerator GoToMap()
	{
		if (!PhotonNetwork.connected)
		{
			controllerConnectPhoton.thisScript._initializeWorldwide();
			while (!PhotonNetwork.connected)
			{
				yield return null;
			}
			settings.updateOfflineMode(false);
		}
		base.gameObject.SetActive(false);
		if (!SceneManager.GetActiveScene().name.Equals("Level1"))
		{
			settings.SetAreaNameForRespawn(SceneManager.GetActiveScene().name);
		}
		else
		{
			settings.SetAreaNameForRespawn(desireMap);
		}
		FyberWrapper.instance.ShowAds(ADSLocation.TransilitionToThePost);
		SceneLoadBar.showHalfBar = false;
		MessageWindow.instance.SetVisiblePanelLoading(true, string.Empty);
		controllerConnectPhoton.thisScript.chooseNameLevel = desireMap;
		controllerConnectPhoton.thisScript.changeMap = true;
		PhotonNetwork.Disconnect();
		while (PhotonNetwork.connected)
		{
			Debug.Log("connected");
			yield return null;
		}
		controllerConnectPhoton.thisScript._initializeWorldwide();
	}
}
